<?php

/**
 * accountSupplier actions.
 *
 * @package    erp-crm
 * @subpackage accountSupplier
 * @author     Milwell H. Sia
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class accountSupplierActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  
  public function executeIndex(sfWebRequest $request){
    $contact_iden = $request->getParameter('iden');
    $contact = ContactPeer::retrieveByPk($contact_iden);
    $this->forward404Unless($contact);
    $accountSupplier = $contact->getAccountSupplier();
    if(! $accountSupplier)
      $this->forward('accountSupplier','create');
    else
      $this->forward('accountSupplier','edit');
  }
  
  public function executeCreate(sfWebRequest $request)
  {
    $contact_iden = $request->getParameter('iden');
    $this->contact = ContactPeer::retrieveByPk($contact_iden);
    $this->forward404Unless($this->contact);
    $this->accountSupplierForm = new AccountSupplierForm();
    $this->accountSupplierForm->setDefault('contact_iden',$contact_iden);
    $this->setLayout(false);
  }
  
  public function executeEdit(sfWebRequest $request)
  {
    $contact_iden = $request->getParameter('iden');
    $this->contact = ContactPeer::retrieveByPk($contact_iden);
    $this->forward404Unless($this->contact);
    $accountSupplier = $this->contact->getAccountSupplier();
    $this->accountSupplierForm = new AccountSupplierForm($accountSupplier);
    $this->setTemplate('create');
    $this->setLayout(false);
  }
  
  public function executeInsert(sfWebRequest $request){
    $account_supplier_params = $request->getParameter('account_supplier');
    $this->contact = ContactPeer::retrieveByPk($account_supplier_params['contact_iden']);
    $this->forward404Unless($this->contact);
    $this->accountSupplierForm = new AccountSupplierForm();
    $this->accountSupplierForm->bind($account_supplier_params);
    if($this->accountSupplierForm->isValid()){
      $this->accountSupplierForm->save();
      $this->redirect('accountSupplier/edit?iden='.$this->accountSupplierForm->getObject()->getContactIden());
    }
    $this->setTemplate('create');
  }
  
  public function executeUpdate(sfWebRequest $request){
    $account_supplier_params = $request->getParameter('account_supplier');
    $this->contact = ContactPeer::retrieveByPk($account_supplier_params['contact_iden']);
    $this->forward404Unless($this->contact);
    $accountSupplier = $this->contact->getAccountSupplier();
    $this->forward404Unless($accountSupplier);
    $this->accountSupplierForm = new AccountSupplierForm($accountSupplier);
    $this->accountSupplierForm->bind($account_supplier_params);
    if($this->accountSupplierForm->isValid()){
      $this->accountSupplierForm->save();
      $this->redirect('accountSupplier/edit?iden='.$accountSupplier->getContactIden());
    }
    $this->setTemplate('create');
  }
  
  public function executeDelete(sfWebRequest $request){
    $contact_iden = $request->getParameter('iden');
    $accountSupplier = AccountSupplierPeer::retrieveByPk($contact_iden);
    $this->forward404Unless($accountSupplier);
    try{
      $accountSupplier->delete();
      $this->redirect('accountSupplier/create?iden='.$contact_iden);
    }catch (Exception $e){
      $request->getParameterHolder()->set('message',$e->getMessage());
      $this->forward('main','error');
    }
  }
  
}
